﻿
if (!window.XMLHttpRequest)
{
    XMLHttpRequest = function() 
    {
        var msxmls = new Array(
          'Msxml2.XMLHTTP.5.0',
          'Msxml2.XMLHTTP.4.0',
          'Msxml2.XMLHTTP.3.0',
          'Msxml2.XMLHTTP',
          'Microsoft.XMLHTTP');
          
        for (var i = 0; i < msxmls.length; i++) 
        {
			try 
			{
				return new ActiveXObject(msxmls[i]);
			} 
			catch (e) {}
		}
		
        return null;
    }
}

var currentLocationResultID = 0;
var locationRequest;
var previousLocation = "";

function STM_locationResultOnClick(locationResult) 
{
	STM_setSelectedLocation(currentLocationResultID);
	STM_submitSelectedLocation();
}

function STM_locationResultMouseOver(locationResult) {
    
    if (currentLocationResultIndex != 0)
        STM_locationResultMouseOut(document.getElementById("locationResult" + currentLocationResultIndex));
    
    locationResult.style.backgroundColor = "#EFEFE0";
    locationResult.style.cursor = "pointer"
    currentLocationResultIndex = parseInt(locationResult.id.substring(14));
    currentLocationResultID = locationResult.getAttribute("lid");
}
	
function STM_locationResultMouseOut(locationResult) {
    locationResult.style.backgroundColor = "white";
    locationResult.style.cursor = "default"
    currentLocationResultIndex = 0;
    currentLocationResultID = 0;
}

function STM_locationRequestResult() {
    if (locationRequest.readyState != 4) 
		return;
		
    if (locationRequest.status != 200) 
		return;
    
    var l = document.getElementById("stmLocation");
    var locationResults = document.getElementById("stmLocationResults");
    var node = l;
    var offsetLeft = node.offsetLeft;
    var offsetTop = node.offsetTop;
    while (node = node.offsetParent) 
    {
        offsetLeft += node.offsetLeft;
        offsetTop += node.offsetTop;
	}
    
    locationResults.innerHTML = locationRequest.responseText;
    
    if (locationRequest.responseText == "")
    {
		locationResults.style.display = "none";
		return;
	}
    
    locationResults.style.display = "";
    if (locationResults.offsetHeight > 400) 
    {
        for(var i = 0; i < 2; i++) 
        {
            var newHeight = 0;
            var c = 0;

            while (newHeight < 400 )//&& locationResults.childNodes.length <c
                newHeight += locationResults.childNodes[c++].offsetHeight;

            locationResults.style.height = newHeight + "px";
		}
	}
    
    locationResults.style.left = offsetLeft + "px";
    locationResults.style.top = offsetTop + l.offsetHeight + 3 + "px";
}

function STM_locationFocus() {
    var location = document.getElementById("stmLocation");
    if (location.value == "Enter City or Zip") {
        location.value = "";
    }
}
	
function STM_locationBlur() {
    var location = document.getElementById("stmLocation");
    if (location.value == "") {
        location.value = "Enter City or Zip";
    }
}
   
function STM_locationChanged() {
    var location = document.getElementById("stmLocation").value;
    
    if (location != previousLocation) 
    {
        previousLocation = location;
        // Check for 3 digits of a zip code or 3 letters of a city
        if (/\d{3,}/.test(location) || (location.length > 2 && /\D{3,}/.test(location)))
        {
			currentLocationResultIndex = 0;
            locationRequest = new XMLHttpRequest();
            locationRequest.onreadystatechange = STM_locationRequestResult;
            // Use GET to cache requests
            // Use POST to not cache requests
            locationRequest.open("GET", "_vti_bin/Demo.SharePoint.WebParts/LocationRequest.aspx?q=" + location + "&r=10&lurl=" + STM_getLocationListUrl(), true);
            locationRequest.send(null);
            return;
		}
    } 
    
	if (location.length < 3)
		$("#stmLocationResults").hide();
}

function STM_clearLocationResults(event) {
    var clearResults = true;
    var p = event.target;
    while(p && p.parentNode) 
    {
        if (p.id == "stmLocationResults" || p.id == "stmLocation") 
        {
            clearResults = false;    
            break;
		}
        
        p = p.parentNode;
    }
    
    if (clearResults)
    {
        $("#stmLocationResults").hide();
        currentLocationResultIndex = 0;
    }
}

var currentLocationResultIndex = 0;
function STM_locationKeyDown(event) {
    var l = document.getElementById("stmLocation");
    var lr = document.getElementById("stmLocationResults");

    var charCode = event.charCode ? event.charCode : event.keyCode;

    if (l.hasFocus == true) 
    {       
        switch(charCode) 
        {
            // down
            case 40:
                var rid = currentLocationResultIndex;
                var nextLocation = document.getElementById("locationResult" + (rid + 1));
                if (nextLocation)
                {
					if (rid != 0)
						STM_locationResultMouseOut(document.getElementById("locationResult" + rid));
					STM_locationResultMouseOver(document.getElementById("locationResult" + (rid + 1)));
				}
                break;
            // up
            case 38:
                var rid = currentLocationResultIndex;
                if (rid > 1) 
                {
                    STM_locationResultMouseOut(document.getElementById("locationResult" + rid));
                    STM_locationResultMouseOver(document.getElementById("locationResult" + (rid - 1)));
				}
                break;
            // esc
            case 27:
                lr.style.display = "none";
                currentLocationResultIndex = 0;
                break;
            // enter
            case 13:
                event.preventDefault();
                STM_locationResultOnClick(document.getElementById("locationResult" + currentLocationResultIndex));
                break;
		}
    }
    
    if (l.hasFocus && charCode == 13) 
    {
        event.preventDefault();
        STM_locationResultOnClick();
    }
    
    if ((charCode == 13) && (eval("typeof(locationQueryID)") != "undefined" || eval("typeof(locationRadiusID)") != "undefined")) 
    {
        event.preventDefault();
        var lq = document.getElementById(locationQueryID);
        if (lq.hasFocus)
            refineSearch();
    }
    if ((charCode == 13) && (eval("typeof(locationRadiusID)") != "undefined")) 
    {
        event.preventDefault();
        var lr = document.getElementById(locationRadiusID);
        if (lr.hasFocus)
            refineSearch();
    }
}

function STM_locationResultDetailedHeadingOnClick(id) {
    var content = document.getElementById("locationResultDetailedContent" + id);
    var image = document.getElementById("locationResultDetailedImage" + id);
    var heading = document.getElementById("locationResultDetailedHeading" + id);
    
    if (content.style.display == "none") 
    {
        content.style.display = "";
        image.src = "_layouts/images/Demo.SharePoint.WebParts/ChevronCollapse.gif";
        image.title = "Click to Collapse Office Information";
        image.alt = "Click to Collapse Office Information";
        heading.className = "stm-officeResultsHeading";
    } 
    else 
    {
        content.style.display = "none";
        image.src = "_layouts/images/Demo.SharePoint.WebParts/ChevronExpand.gif";
        image.title = "Click to Expand Office Information";
        image.alt = "Click to Expand Office Information";
        heading.className = "stm-officeResultsHeadingClosed";
	}
}

$(document).click(STM_clearLocationResults);
$(document).keydown(STM_locationKeyDown);
